using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PicoInterface;

namespace RapidHDL
{
    public partial class FSignals : Form
    {
        HardwareTreeVeiw oHardwareTreeView;
        RapidHardware oRapidHardware;
        MicroTraceData oMicroTraceData;

        public FSignals()
        {
            InitializeComponent();
        }

        public void Initialize(RapidHardware poRapidHardware)
        {
            oRapidHardware = poRapidHardware;

            // get the microcore defition
            // override the rapid hardware dashboard
            // create the dashboard
            ctlChip.AttachMicroTrace(ctlTrace);            
            ctlChip.RapidHardware = poRapidHardware;

            tvHardware.ImageList = imgList;
            TreeNode tnRoot = tvHardware.Nodes.Add("Root","Root",22);
            oHardwareTreeView = new HardwareTreeVeiw(oRapidHardware, tvHardware,tvHardware.Nodes[0],23);
            oHardwareTreeView.ReloadTree();

            tnRoot.Expand();
            oHardwareTreeView.ExpandSignalView();

            oMicroTraceData = oRapidHardware.GetMicroTraceData();
            if (oMicroTraceData.IsValid)
            {
                ctlChip.AttachMicro(oMicroTraceData.IRView,oMicroTraceData.PCView);
                ctlTrace.SetProgramData(oMicroTraceData.ProgramText, oMicroTraceData.AddressToLine, oMicroTraceData.Breakpoints, oMicroTraceData.OpCodes);
            }

            ActionCaptions();

            //    ctlTrace.SetProgramData(oSNN.GetProgramText(), oSNN.GetProgramAddressToLine(), oSNN.GetBreakpoints(), oSNN.GetOpCodes());
            //    oSNN.LoadRAMProgram();

            UCTestInterface oTestInterface = oRapidHardware.GetTestInterface();
            if (oTestInterface != null)
            {
                oTestInterface.Location = new Point(cmdDock.Left, cmdDock.Top);
                //oTestInterface.Width = this.Width;
                //oTestInterface.Height = this.Height;
                this.Controls.Add(oTestInterface);
                oTestInterface.SetRapidHardware(oRapidHardware);
                oTestInterface.Visible = true;
                splitContainer3.Panel1.Controls.Add(oTestInterface);
                oTestInterface.BringToFront();
            }

            oRapidHardware.LoadRAM();
        }

        private void FSignals_Load(object sender, EventArgs e)
        {            
        }

        private void tvHardware_AfterSelect(object sender, TreeViewEventArgs e)
        {
            SelectHardwareNode(tvHardware.SelectedNode);
        }

        private void ctlChip_Load(object sender, EventArgs e)
        {
            
        }

        private void ctlChip_Updated()
        {
            //if (oHardwareTreeView != null)
            //    oHardwareTreeView.RefreshTree();
            tmrTranscript_Tick(null, null);
        }


        private void SelectHardwareNode(TreeNode ptnNode)
        {
            bool bShowProperties = true;
            RapidHDL.SelectedHardwareType selectedType = oHardwareTreeView.GetSelectedHardwareType(ptnNode);
            object oSelected = ptnNode.Tag;

            switch (selectedType)
            {
                case SelectedHardwareType.Component:
                    break;
                case SelectedHardwareType.Root:
                    break;
                case SelectedHardwareType.NVPassthrough:
                    break;
                case SelectedHardwareType.NVSink:
                    break;
                case SelectedHardwareType.NVSource:
                    break;
                case SelectedHardwareType.TopHardware:
                    break;
                case SelectedHardwareType.View:
                    break;
                case SelectedHardwareType.Source:
                    break;
                case SelectedHardwareType.Verilog:
                    cbTrace.Checked = false;
                    cbTranscript.Checked = false; 
                    ComponentVerilog oVerilog = (ComponentVerilog)oSelected;
                    ctlTextView.LoadVerilog(oVerilog);
                    break;
                case SelectedHardwareType.Log:
                    cbTrace.Checked = false;
                    cbTranscript.Checked = false;
                    ctlTextView.LoadLogFile((string)oSelected);
                    break;
                default:
                    bShowProperties = false;
                    break;
            }

            if (bShowProperties)
            {
                pgView.SelectedObject = oSelected;
            }
        }

        private void tmrTranscript_Tick(object sender, EventArgs e)
        {
            if (cbTranscript.Checked)
            {
                if (oRapidHardware.Transcript.Changed)
                {
                    ctlTextView.ShowTranscript(oRapidHardware.Transcript.GetTranscript());
                }
            }
        }

            
        private void button1_Click(object sender, EventArgs e)
        {
            TestPCSDRAM.PCTest();
            //TestPCIO.PCIOPerSec();
            //TestFIFOInterface.PCTestPost();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //TestPCIO.PCTest();
            //return;
            
            /*FQuickFabric fFabric = new FQuickFabric();
            fFabric.oRapidHardware = oRapidHardware;
            fFabric.ShowDialog();*/

            IOMemoryTest.TestMemory(1000000, 1000600);
            IOMemoryTest.TestMemory(2000000,2500000);
            IOMemoryTest.TestMemory(200, 300);
            for (int iBlock = 0; iBlock < 30; iBlock++)
            {
                int iStart = iBlock * 1000000 + 100000;
                IOMemoryTest.TestMemory(iStart, iStart + 100);
            }
            IOMemoryTest.TestMemory(4194300, 4194400);
            IOMemoryTest.TestMemory(4000000, 10000000);
            IOMemoryTest.TestMemory(200, 300);
            
            //TestPCIO.PCTest();
            //TestFIFOInterface.PCTestRead();
            //TestFIFOInterface.PCTest();
            //MessageBox.Show(Conversion.IntToBinaryString(PicoIO.ReadIOStatusReg()));
        }

        private void cbTrace_CheckedChanged(object sender, EventArgs e)
        {
            cbTranscript.Checked = !cbTrace.Checked;
            if (cbTrace.Checked)
            {
                ctlTrace.Visible = true;

            }
            else
            {
                ctlTrace.Visible = false;
            }
        }


        private void cmdStartRAM_Click(object sender, EventArgs e)
        {
            //IOSpikingNetwork.MainReset();

            /*FPGABlock oBlock = new FPGABlock();
            oBlock.LoadA(33);
            IOInterface.PostFPGA(oBlock);*/


            //if (!oSNN.StartRAMOnFPGA())
            //    System.Windows.Forms.MessageBox.Show("ERROR starting SNN RAM Program");
            //else
            //oSNN.LoadRAMProgram();
            //    System.Windows.Forms.MessageBox.Show("OK");
        }

        private void cmdCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(oRapidHardware.Transcript.GetTranscript());
        }

        private void ActionCaptions()
        {
            string sCaption = oRapidHardware.GetActionCaption(1);
            cmd1.Text = sCaption;
            if (sCaption == "")
                cmd1.Visible = false;
            sCaption = oRapidHardware.GetActionCaption(2);
            cmd2.Text = sCaption;
            if (sCaption == "")
                cmd2.Visible = false;
            sCaption = oRapidHardware.GetActionCaption(3);
            cmd3.Text = sCaption;
            if (sCaption == "")
                cmd3.Visible = false;
        }

        private void cmd1_Click(object sender, EventArgs e)
        {
            oRapidHardware.DoAction(1);
        }

        private void cmd2_Click(object sender, EventArgs e)
        {
            oRapidHardware.DoAction(2);
        }

        private void cmd3_Click(object sender, EventArgs e)
        {
            oRapidHardware.DoAction(3);
        }

        private void cmdDock_Click(object sender, EventArgs e)
        {

        }

        private void FSignals_FormClosing(object sender, FormClosingEventArgs e)
        {
            oHardwareTreeView.UnregisterTreeView();
        }



    }
}